package com.open.base.utils;


import com.open.base.model.LoginUser;
import com.open.base.model.Menu;
import com.open.base.model.Role;
import com.open.base.model.User;
import com.open.common.utils.StringUtils;

import java.util.List;

/**
 * 用户信息工具类
 * @author dashu03
 * @date 2023/9/30 8:56
 */
public class UserHolderUtils {
    public static final ThreadLocal<LoginUser> managerThreadLocal = new ThreadLocal<>();

    public static void saveLoginUser(LoginUser user) {
        managerThreadLocal.set(user);
    }

    public static LoginUser getLoginUser() {
        return managerThreadLocal.get();
    }

    public static void removeLoginManager() {
        managerThreadLocal.remove();
    }

    public static User getUser() {
        return getLoginUser().getUser();
    }

    //获取当前用户Id
    public static Long getUserId() {
        return getLoginUser().getUser().getId();
    }

    // 获取当前用户角色Id
    public static Role getRole() {
        return getLoginUser().getRole();
    }

    //获取当前用户名称
    public static String getUserName() {
        return getLoginUser().getUser().getUsername();
    }

    //获取当前用户名
    public static String getName() {
        return getLoginUser().getUser().getName();
    }

    //获取当前权限
    public static List<Menu> getPermissions() {
        return getLoginUser().getPermissions();
    }

    //获取当前用户
    public static List<Role> getRoles() {
        return getLoginUser().getRoles();
    }

    //获取当前唯一令牌
    public static String getToken() {
        if (StringUtils.isNotNull(getLoginUser())) {
            return getLoginUser().getToken();
        }
        return null;
    }
}